package com.skin.admin.manager;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.skin.admin.entity.Roleperm;
import com.skin.admin.entity.Roles;
import com.skin.admin.entity.Userrole;
import com.skin.admin.serviceImpl.RolepermServiceImpl;
import com.skin.admin.serviceImpl.RolesServiceImpl;
import com.skin.admin.serviceImpl.UserroleServiceImpl;
import com.skin.configuration.shiro.ShiroLoad;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/7/20 0020.
 */
@Component
public class RoleManager {

    protected static final Logger log = LoggerFactory.getLogger(RoleManager.class);

    @Autowired
    private UserroleServiceImpl userroleService;

    @Autowired
    private RolesServiceImpl rolesService;

    @Autowired
    private RolepermServiceImpl rolepermService;

    public Roles findBy(String colum,String val){
        return rolesService.selectOne(new EntityWrapper<Roles>().eq(colum,val));
    }

    /**
     * 查询出角色的多条信息
     * @param colum
     * @param val
     * @return
     */
    public List<Roles> findListBy(String colum,String val){
        return rolesService.selectList(new EntityWrapper<Roles>().eq(colum,val));
    }

    /**
     * 通过用户id查询出用户角色表关联数据
     * @param colum
     * @param val
     * @return
     */
    public List<Userrole> findUserroleBy(String colum,String val){
        return userroleService.selectList(new EntityWrapper<Userrole>().eq(colum,val));
    }

    public List<Roles> All(){
        return rolesService.selectList(new EntityWrapper<Roles>());
    }

    /**
     * 全部数据 分页
     * @param size
     * @param page
     * @return
     */
    public Page<Roles> All(int size ,int page){
        return rolesService.selectPage(new Page<>(size,page),new EntityWrapper<Roles>().ne("name","admin"));
    }

    @Transactional
    public void deleteById(int id){

        //删除关联在 删除自身
        userroleService.delete(new EntityWrapper<Userrole>().eq("roleid",id));
        rolepermService.delete(new EntityWrapper<Roleperm>().eq("roleid",id));
        rolesService.deleteById(id);

    }

    public void edit(Roles roles){
        if(roles!=null){
            rolesService.updateById(roles);
        }
    }
    public void insert(Roles roles){
        if(roles!=null){
           List<Roles> size= rolesService.selectList(new EntityWrapper<Roles>().eq("name",roles.getName()));
           if(size.size()>0){
               log.info("存在多个角色");
           }else{
               rolesService.insert(roles);
           }
        }
    }
}
